﻿@page "/Admin/Timers"
@attribute [Authorize]
@attribute [TabItemOption(Text ="任务管理",Icon ="fa fa-th")]
@inherits BasePage
@inject ISysTimerService SysTimerService

<div class="overflow-hidden h-100">
    <Table TItem="JobOutput"
            @ref = "@_table"
            IsPagination="true"
            IsStriped="true"
            IsMultipleSelect="true"
            IsBordered="false"
            IsFixedHeader="true" 
            AllowResizing="true"
            ShowLoading="true"
            ShowColumnList="true"
            ShowCardView="true"
            ShowToolbar="true"
            ShowSearch="true"
            ShowExtendButtons="true" 
            ShowEmpty="true" 
            SearchModel="@_search" 
            SearchMode="SearchMode.Top"
            EditDialogItemsPerRow="1"
            EditDialogSize="Size.Medium"
            ExtendButtonColumnWidth="250"
            PageItemsSource="@(new int[] { 15, 50 })"
            OnQueryAsync="@OnQueryAsync"
            OnSaveAsync="@OnSaveAsync"
            OnDeleteAsync="@OnDeleteAsync">
        <TableColumns>
            <TableColumn @bind-Field="@context.JobName" />
            <TableColumn @bind-Field="@context.RequestUrl" />
            <TableColumn @bind-Field="@context.RequestParameters"/>
            <TableColumn @bind-Field="@context.Interval" />
            <TableColumn @bind-Field="@context.Cron" />
            <TableColumn @bind-Field="@context.TimerStatus" />
            <TableColumn @bind-Field="@context.RunNumber" />
            <TableColumn @bind-Field="@context.Remark" />
        </TableColumns>
        <EditTemplate>
            <ConditionRender Watch="@context" Condition="true" Name="局部渲染用">
                <div class="row g-3 form-inline">
                    <div class="col-sm-6">
                        <BootstrapInput @bind-Value="@context.JobName" maxlength="50" />
                    </div>

                    <div class="col-sm-6">
                        <BootstrapInput @bind-Value="@context.RequestUrl" maxlength="50" />
                    </div>

                    <div class="col-sm-12">
                        <RadioList maxlength="50" 
                            @bind-Value="@context.RequestType" 
                            Items="@_requestTypes"  
                            OnSelectedChanged="((vs,v)=> context.ChangedAsync())"/>
                    </div>

                    <ConditionRender Condition="@(context.RequestType != RequestTypeEnum.Run)">
                        <div class="col-sm-12">
                            <BootstrapInput @bind-Value="@context.Headers" PlaceHolder="请输入请求头" maxlength="50" />
                        </div>
                    </ConditionRender>

                    <div class="col-sm-12">
                        <BootstrapInput maxlength="50"
                            @bind-Value="@context.RequestParameters" 
                            DisplayText="@(context.RequestType == RequestTypeEnum.Run?"配置项参数":"请求参数")"
                            PlaceHolder="@(context.RequestType == RequestTypeEnum.Run?"请输入配置项参数":"请输入请求参数")"  />
                    </div>

                    <div class="col-sm-6">
                        <Select 
                            @bind-Value="@context.TimerType" 
                            Items ="@_timerTypes"
                            OnSelectedItemChanged="((v)=> context.ChangedAsync())"  />
                    </div>

                    <div class="col-sm-6">
                        <ConditionRender Condition="@(context.TimerType == SpareTimeTypes.Interval)">
                            <TrueContent>
                                <BootstrapInputNumber @bind-Value="@context.Interval"  />
                            </TrueContent>
                            <FalseContent>
                                <BootstrapInput @bind-Value="@context.Cron"  />
                            </FalseContent>
                        </ConditionRender>
                    </div>

                    <div class="col-sm-6">
                        <Switch @bind-Value="@context.StartNow" OnText="是" OffText="否"/>
                    </div>

                    <div class="col-sm-6">
                        <Switch @bind-Value="@context.DoOnce" OnText="是" OffText="否"/>
                    </div>

                    <div class="col-sm-6">
                        <Select @bind-Value="@context.ExecuteType" Items="@_executeTypes" />
                    </div>

                    <div class="col-sm-6">
                        <BootstrapInput @bind-Value="@context.Remark" maxlength="50" />
                    </div>
                </div>
            </ConditionRender>
        </EditTemplate>
        <RowButtonTemplate>        
            <PopConfirmButton Size="Size.ExtraSmall" 
                Text="@(context.TimerStatus == SpareTimeStatus.Running?"停止":"启动")" 
                Content="@(context.TimerStatus == SpareTimeStatus.Running?"是否停止任务？":"是否启动任务？")" 
                OnConfirm="@(()=>OnExecuteAsync(context))" />
        </RowButtonTemplate>
        <SearchTemplate>
            <GroupBox Title="搜索条件">
                <div class="row g-3 form-inline">
                    <div class="col-12 col-sm-6">
                        <BootstrapInput @bind-Value="@context.JobName" ShowLabel="true"/>
                    </div>
                </div>
            </GroupBox>
        </SearchTemplate>
    </Table>
</div>

@code{
    private Table<JobOutput>? _table;
    private JobOutput _search = new();

    private IEnumerable<SelectedItem>? _requestTypes;
    private IEnumerable<SelectedItem>? _timerTypes;
    private IEnumerable<SelectedItem>? _executeTypes;

    protected override void OnInitialized()
    {
        base.OnInitialized();

        _requestTypes = typeof(RequestTypeEnum).ToSelectList();
        _timerTypes = typeof(SpareTimeTypes).ToSelectList();
        _executeTypes = typeof(SpareTimeExecuteTypes).ToSelectList();
    }

    private Task<QueryData<JobOutput>> OnQueryAsync(QueryPageOptions options)
        => SysTimerService.PageAsync(_search.Adapt<TimerPageInput>(options));

    private Task<bool> OnDeleteAsync(IEnumerable<JobOutput> items) 
        => SysTimerService.DeleteAsync(items);

    private async Task<bool> OnSaveAsync(JobOutput item, ItemChangedType type)
    {
        var result = false;
        switch (type)
        {
            case ItemChangedType.Add:
                result = await SysTimerService.AddAsync(item);
                break;
            case ItemChangedType.Update:
                result = await SysTimerService.EditAsync(item);
                break;
        }
        return result;
    }

    private async Task OnExecuteAsync(JobOutput item)
    {
        if (item.TimerStatus == SpareTimeStatus.Running)
        {
            await SysTimerService.StopAsync(item);
        }
        else
        {
            await SysTimerService.StartAsync(item);
        }
        await _table!.QueryAsync();
    }
}

